{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "419b9401",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasets import load_dataset\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import classification_report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4dbe1773",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_train_test_dataset_pd(train_split=\"train[:15%]+train[-15%:]\", test_split=\"test[:15%]+test[-15%:]\"):\n",
    "    train_dataset = load_dataset(\"rotten_tomatoes\", split=train_split)\n",
    "    test_dataset = load_dataset(\"rotten_tomatoes\", split=test_split)\n",
    "    train_df = train_dataset.to_pandas()\n",
    "    train_df.sample(frac=1)\n",
    "    test_df = test_dataset.to_pandas()\n",
    "    return (train_df, test_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "80968641",
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_train_test_data(train_df, test_df, vectorize, column_name=\"text\"):\n",
    "    train_df[\"vector\"] = train_df[column_name].apply(lambda x: vectorize(x))\n",
    "    test_df[\"vector\"] = test_df[column_name].apply(lambda x: vectorize(x))\n",
    "    X_train = np.stack(train_df[\"vector\"].values, axis=0)\n",
    "    X_test = np.stack(test_df[\"vector\"].values, axis=0)\n",
    "    y_train = train_df[\"label\"].to_numpy()\n",
    "    y_test = test_df[\"label\"].to_numpy()\n",
    "    return (X_train, X_test, y_train, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1a0a818a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_classifier(X_train, y_train):\n",
    "    clf = LogisticRegression(C=0.1)\n",
    "    clf = clf.fit(X_train, y_train)\n",
    "    return clf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "35a0a248",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_classifier(test_df, clf, target_names=None):\n",
    "    test_df[\"prediction\"] = test_df[\"vector\"].apply(lambda x: clf.predict([x])[0])\n",
    "    print(classification_report(test_df[\"label\"], test_df[\"prediction\"], target_names=target_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b07a9731",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
